package com.ss.project.xia17user.common.utils;

import com.ss.project.xia17user.core.exceptions.BadRequestException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 请求返回工具
 * @author xia17
 * @date 2020/11/23 17:44
 */
@Slf4j
public class ResponseUtil {


    /**
     * 报错信息
     * @param response 返回体
     * @param status http状态
     * @param message 消息
     */
    public static void sendError(HttpServletResponse response, HttpStatus status , String message){
        log.error("使用response.sendError()来报错.");
        log.error("报错提示::{}",message);
        try {
            response.sendError(status.value(),message);
        } catch (IOException e) {
            e.printStackTrace();
            throw new BadRequestException("response.sendError错误",e);
        }
    }

    /**
     * 错误信息
     * @param request 请求
     * @param response 返回
     * @param status 状态
     * @param throwable 异常
     */
    public static void sendError(HttpServletRequest request, HttpServletResponse response , Throwable throwable , HttpStatus status){
        log.error("使用response.sendError()来报错.");
        log.error("引起报错的异常详情::",throwable);
        RequestUtil.setErrorAttribute(request,throwable);
        try {
            response.sendError(status.value());
        } catch (IOException e) {
            e.printStackTrace();
            throw new BadRequestException("response.sendError错误",e);
        }
    }


    /**
     * 错误信息
     * @param request 请求
     * @param response 返回
     * @param throwable 异常
     */
    public static void sendError(HttpServletRequest request, HttpServletResponse response ,  Throwable throwable){
        sendError(request,response,throwable,HttpStatus.BAD_REQUEST);
    }





}
